package day01;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/sayHelloAnnotation")
public class HelloServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("HelloServlet.doGet");
//    }

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("HelloServlet.doPost");
//    }


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("HelloServlet.service");

        /*
            解析请求体使用的编码是utf-8
         */
        request.setCharacterEncoding("utf-8");

        /*
            告诉容器，响应体中的数据表示是utf-8编码
         */
//        response.setCharacterEncoding("utf-8");

        /*
            告诉容器，响应体中的数据格式是text/html(文本/html格式),字符编码是utf-8
            以utf-8编码传输给客户端
            告诉浏览器使用utf-8编码解析响应体数据并打开
         */
        response.setContentType("text/html;charset=utf-8");


//        int i=1/0;

        String name=request.getParameter("name");

//        name=new String(name.getBytes("ISO8859-1"),"utf-8");

        System.out.println("name = " + name);

        PrintWriter out=response.getWriter();

//        out.println("<h1 style='color:red;'>Hello World!</h1>");
        out.println("<h1 style='color:red;'>你好 "+name+"!</h1>");
    }
}


